<?
require '../inc.php';
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0

$cacheTime      = 300; //data update interval

$arr	  		= pct::de($_GET['id']);
$id	  			= $arr["date"];
$times    		= max((int)$_GET['times'],1);
$save     		= intval($_GET['save']);//加了一个save db的方法
$table    		= "goods_info_times";
$time           = time();
$whistoryview 	= "whystoryview";
$history        = explode('-', $_COOKIE[$whistoryview]);//array_diff(explode('-', $_COOKIE[$whistoryview]), array ($id));
array_unshift($history, $id);
$history        = array_slice(array_unique($history), 0, 20);
g::setcookie($whistoryview, implode('-', $history), 1000);

//times
$ca = new ca($id,"pct_stat",0,1000);
if ($_GET['dy'] == 'c')
{
    $ca->del();
	$stat=false;
}else
{
	$stat=$ca->get();
}
//下面上数据处理
$where    = array(
	"where"=>" gYmd =:gYmd and gid =:gid ",
	"bind"=>$arr
);
if (empty($stat) || empty($stat['data']['times']) || empty($stat['MTime'])) 
{
	$db_goods = new db($scfg['db']['goods']);
	$rs= $db_goods->row("select `times` from {$table} where {$where['where']} ", $where["bind"]);
	//echo "/* ".$db_goods->getsql()." */";
    if(!$rs)
    {
        $rs = $db_goods->row("select `gYmd`,`gid`,`urs` from goods_info  where {$where['where']} ", $where["bind"]);
    	$db_goods->insert($table,$rs);
    }
    $rs['times']++;

    $stat['tt']          = 1;
    $stat['MTime']       = $time;
    $stat['data']        = $rs;
    $stat['update'] 	 = 0;
}else 
{
    if (($stat['MTime']+$cacheTime<$now && $stat['tt']>1) || $save==6917) 
    {
        $tt   = $times?$times : 1;//设定step 
        $tt_t =  rand(20, 30)+$tt;//加权
        
        $stat['data']['times'] += $tt_t;
        if (!$db_goods) 
        	$db_goods = new db($scfg['db']['goods']);
        $db_goods->add($table, array ('times' => $stat['tt']+$tt_t,'reality_times'=>$stat['tt']+$tt), $where['where'], $where["bind"]);
        //echo "/* ".$db_goods->getsql()." */";
        $stat['tt']= 0;
        $stat['MTime']= $time;
        $stat['update']++;
        if($stat['update']>5)
        	$ca->del();
    }else 
    {
        $tt= $times?$times : 1;//设定step 
        $stat['data']['times'] += $tt;
        $stat['tt'] += $tt;
    }
}
$ca->put($stat);
echo json::flush($stat['data']);
?>